package com.cetcs.kmga.dataManager.service.impl.dataManager;

import com.cetc.cloud.kmga.util.DateUtils;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.common.Page;
import com.cetcs.kmga.dataManager.dao.mybatis.AuditManagerMapper;
import com.cetcs.kmga.dataManager.entity.dataManager.ClientDB;
import com.cetcs.kmga.dataManager.entity.dataManager.Resrc;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.*;
import com.cetcs.kmga.dataManager.service.dataManager.TrafficStatisticService;
import com.cetcs.kmga.dataManager.util.JsonUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * 流量统计Service    可删
 *
 * @author zhangchunlin
 * @version V1.0 创建时间：2017-10-09 14:56
 * Copyright 2017 by CETC
 */
@Service("trafficStatisticService")
public class TrafficStatisticServiceImpl implements TrafficStatisticService {

    @Autowired
    private AuditManagerMapper auditManagerMapper;

    @Resource(name = "trafficStatisticRealService",type = TrafficStatisticService.class)
    private TrafficStatisticService trafficStatisticServiceRealImpl;

    //数据库中是否有真实数据,如果有就从数据库查询数据,否则使用模拟数据
    private boolean hasDataInTable = false;

    @Override
    public Page findResourcetraffic(int pageNumber, int pageSize, String keyword,String id){
        Page dbRet = trafficStatisticServiceRealImpl.findResourcetraffic(pageNumber,pageSize,keyword,id);
        if(dbRet != null && dbRet.getContent() != null && dbRet.getContent().size()>0){
//            hasDataInTable = true;
//            return dbRet;
        }
        hasDataInTable = false;

        String json = "[" +
                "{\"id\":\"1\",\"name\":\"HBASE\",\"ip\":\"172.191.223.15\",\"resrcTypeName\":\"HBASE\"}," +
                "{\"id\":\"2\",\"name\":\"HBASE\",\"ip\":\"172.191.223.124\",\"resrcTypeName\":\"HBASE\"}," +
                "{\"id\":\"3\",\"name\":\"HBASE\",\"ip\":\"172.191.223.138\",\"resrcTypeName\":\"应用\"}," +
                "{\"id\":\"4\",\"name\":\"HBASE\",\"ip\":\"172.191.223.15\",\"resrcTypeName\":\"HBASE\"}," +
                "{\"id\":\"5\",\"name\":\"门户1\",\"ip\":\"172.191.223.15\",\"resrcTypeName\":\"应用\"}," +
                "{\"id\":\"6\",\"name\":\"门户1\",\"ip\":\"172.191.223.15\",\"resrcTypeName\":\"ORALCE\"}," +
                "{\"id\":\"7\",\"name\":\"门户1\",\"ip\":\"172.191.223.15\",\"resrcTypeName\":\"HBASE\"}," +
                "{\"id\":\"8\",\"name\":\"门户1\",\"ip\":\"172.191.223.15\",\"resrcTypeName\":\"ORALCE\"}," +
                "{\"id\":\"9\",\"name\":\"Hbase\",\"ip\":\"172.191.223.15\",\"resrcTypeName\":\"HBASE\"}," +
                "{\"id\":\"10\",\"name\":\"Hbase\",\"ip\":\"172.191.223.15\",\"resrcTypeName\":\"HBASE\"}" +

                "]";
        List<ResourceTrafficVo> mocks = (List<ResourceTrafficVo>)JsonUtil.jsonToList(json,ResourceTrafficVo.class);
        for(ResourceTrafficVo v : mocks){
            mockStatisticInfo(v);
        }
        Page ret = new Page();
        ret.setContent(mocks);
        ret.setTotalPageCount(3);
        ret.setTotalRecord(mocks.size());

        return ret;
    }

    private void mockStatisticInfo(ResourceTrafficVo vo){
        int id = vo.getId();
        id = id < 100? 50 : id;
        Random random = new Random();
        vo.setClientIpAmout(random.nextInt(id)%(101));

        vo.setOutNum(random.nextInt(300),random.nextInt(300));
        vo.setInNum(random.nextInt(1235),random.nextInt(1235));
        vo.setTotalNum(random.nextInt(1352),random.nextInt(1352));
    }

    @Override
    public List<HistogramChartVo> findTrafficStatistic(Integer resourceId){
        if(hasDataInTable){
            List<HistogramChartVo> dbRet = trafficStatisticServiceRealImpl.findTrafficStatistic(resourceId);
            return dbRet;
        }

        List<HistogramChartVo> ret = new LinkedList<>();
        List<String> xaxis = Arrays.asList("今日","平均","峰值");
        List<String> titles = Arrays.asList("读取流量","写入流量","访问IP量");
        Random random = new Random();
        for(String title : titles) {
            HistogramChartVo item = new HistogramChartVo();
            item.setName(title);
            item.setXaxis(xaxis);

            int s =random.nextInt(300);
            item.setYdata(Arrays.asList( String.valueOf(s), "170",  String.valueOf(s+170)));
            ret.add(item);
        }

        return ret;
    }

    @Override
    public MultiLineChartVo findAccessAmount(int timeCycle,Integer resourceId){
        if(hasDataInTable){
            MultiLineChartVo dbRet = trafficStatisticServiceRealImpl.findAccessAmount(timeCycle,resourceId);
            return dbRet;}
        return mockMultiLineChartVo(timeCycle);
    }

    @Override
    public MultiLineChartVo findAccessTimes(int timeCycle,Integer resourceId){
        if(hasDataInTable){
            MultiLineChartVo dbRet = trafficStatisticServiceRealImpl.findAccessTimes(timeCycle,resourceId);
            return dbRet;}
        return mockMultiLineChartVo(timeCycle);
    }

    /**
     * 随机生成一个MultiLineChartVo
     * @return
     */
    MultiLineChartVo mockMultiLineChartVo(int timeCycle){
        MultiLineChartVo ret = new MultiLineChartVo();
        ret.setName("流量趋势");
        switch (timeCycle){
            case 1:{//小时
                ret.setxAxis(Arrays.asList("01:00","02:00","03:00","04:00","05:00",
                        "06:00","07:00","08:00","09:00","10:00","11:00","12:00","13:00","04:00","15:00"));
                break;
            }
            case 2:{//天
                ret.setxAxis(Arrays.asList("2017/2/20","2017/2/22","2017/2/24","2017/2/28","2017/3/2",
                        "2017/3/6","2017/3/8","2017/3/10","2017/3/14","2017/3/16","2017/3/20","2017/3/22","2017/3/24","2017/3/28","2017/3/30"));
                break;
            }
            case 3:{//月
                ret.setxAxis(Arrays.asList("2017/01","2017/02","2017/03","2017/04","2017/05",
                        "2017/06","2017/07","2017/08","2017/09","2017/10","2017/11","2017/12"));
                break;
            }
        }


        int len = ret.getxAxis().size();
        double min = 0.978;
        double max = 1.025;
        List<MultiLineChartVo.YdataFormat> datas = new LinkedList<>();
        ret.setDatas(datas);
        MultiLineChartVo.YdataFormat data1 = new MultiLineChartVo.YdataFormat();
        data1.setName("读取");
        data1.setData(mockYdataFormat(len,min,max));
        datas.add(data1);

        MultiLineChartVo.YdataFormat data2 = new MultiLineChartVo.YdataFormat();
        data2.setName("写入");
        data2.setData(mockYdataFormat(len,min,max));
        datas.add(data2);
        return  ret;
    }



    /**
     * 随机生成一个MultiLineChartVo.YdataFormat
     * @param len
     * @return
     */
    public static List<String> mockYdataFormat(int len,double min,double max){
        List<String> ret = new LinkedList<>();
        Random random = new Random();
        for(int i = 0;i<len;i++){
            ret.add(String.valueOf(random.nextDouble()%(max) + min));
        }

        return ret;
    }

    /**
     * 生成一段时间字符串列表<br>
     * 如果startDate为空,默认为前30天,endDate为空,默认前一天
     * @return
     */
    public static  List<String> mockDateString(String startDate, String endDate){
        if(startDate == null && endDate == null){
            return Arrays.asList("2017/2/20","2017/2/22","2017/2/24","2017/2/28","2017/3/2",
                    "2017/3/6","2017/3/8","2017/3/10","2017/3/14","2017/3/16","2017/3/20","2017/3/22","2017/3/24","2017/3/28","2017/3/30");
        }
        if(startDate == null|| startDate.equals("")){
            startDate = "2017-02-20";
        }
        if(endDate == null|| endDate.equals("")){
            endDate = "2017-03-30";
        }
        List<String> ret = new LinkedList<>();
        List<String> days = DateUtils.getBetweenDatas(startDate,endDate);
        for(String day : days){
            ret.add(day.replace("-","/"));
        }
        return ret;
    }

    /**
     *  模型中两个折线图的都是最小值和最大值相同,所以抽取了这个方法,下次可能需要删除,有2处调用
     * @return
     */
    public static List<MultiLineChartVo.YdataFormat> mockDatas(List<String> titles,MultiLineChartVo ret){
        int len = ret.getxAxis().size();
        List<MultiLineChartVo.YdataFormat> datas = new LinkedList<>();
        for(String title : titles) {
            MultiLineChartVo.YdataFormat data1 = new MultiLineChartVo.YdataFormat();
            data1.setName(title);
            data1.setData(TrafficStatisticServiceImpl.mockYdataFormat(len,0.978,1.013));
            datas.add(data1);
        }
        return datas;
    }

    @Override
    public Page findIpOfResource(int resrc_id,String type,int pageNumber, int pageSize,String keyword){
        if(hasDataInTable){
        Page dbRet = trafficStatisticServiceRealImpl.findIpOfResource(resrc_id,type,pageNumber, pageSize,keyword);
        return dbRet;}

        String json = "[" +
                "{\"serId\":\"1\",\"clientIp\":\"172.191.223.15\",\"outNum\":\"300万条\",\"inNum\":\"1325万条\",\"totalNum\":\"1325万条\"}," +
                "{\"serId\":\"2\",\"clientIp\":\"172.191.223.124\",\"outNum\":\"300万条\",\"inNum\":\"1325.2万条\",\"totalNum\":\"1325.2万条\"}," +
                "{\"serId\":\"3\",\"clientIp\":\"172.191.223.138\",\"outNum\":\"300万条\",\"inNum\":\"1325万条\",\"totalNum\":\"1325万条\"}," +
                "{\"serId\":\"4\",\"clientIp\":\"172.191.223.15\",\"outNum\":\"300万条\",\"inNum\":\"1325.2万条\",\"totalNum\":\"1325.2万条\"}," +
                "{\"serId\":\"5\",\"clientIp\":\"172.191.223.15\",\"outNum\":\"300万条\",\"inNum\":\"1325万条\",\"totalNum\":\"1325万条\"}," +
                "{\"serId\":\"6\",\"clientIp\":\"172.191.223.15\",\"outNum\":\"300万条\",\"inNum\":\"1325.2万条\",\"totalNum\":\"1325.2万条\"}," +
                "{\"serId\":\"7\",\"clientIp\":\"172.191.223.15\",\"outNum\":\"300万条\",\"inNum\":\"1325万条\",\"totalNum\":\"1325万条\"}," +
                "{\"serId\":\"8\",\"clientIp\":\"172.191.223.15\",\"outNum\":\"300万条\",\"inNum\":\"1325.2万条\",\"totalNum\":\"1325.2万条\"}," +
                "{\"serId\":\"9\",\"clientIp\":\"172.191.223.15\",\"outNum\":\"300万条\",\"inNum\":\"1325万条\",\"totalNum\":\"1325万条\"}," +
                "{\"serId\":\"10\",\"clientIp\":\"172.191.223.15\",\"outNum\":\"300万条\",\"inNum\":\"1325.2万条\",\"totalNum\":\"1325.2万条\"}" +

                "]";

        List<?> list = JsonUtil.jsonToList(json,TrafficResrcIpTodayView.class);
        Page ret = new Page();
        ret.setContent(list);
        ret.setTotalRecord(pageSize * 4);
        ret.setTotalPageCount(1);
        ret.setPageNumber(3);
        return ret;
    }

    //===============================yyangs start====================//

    /**
     * 获取数据库直连客户端
     * @param dataMap
     */
    @Override
    public ClientForDBVo getClientListForDatabaseDirectConnection(Data dataMap) {
        ClientForDBVo clientForDBVo = new ClientForDBVo();
        //合法
        ClientTreeForDBVo clientTreeForDBVo = new ClientTreeForDBVo();
        clientTreeForDBVo.setId("A1");
        clientTreeForDBVo.setName("数据库直连终端（合法）");
        clientTreeForDBVo.setNumber(3);
        List<ResourceTreeDataObjectVo> list = Lists.newArrayList();
        ResourceTreeDataObjectVo resourceTreeDataObjectVo = new ResourceTreeDataObjectVo();
        resourceTreeDataObjectVo.setId("B1");
        resourceTreeDataObjectVo.setName("办公厅301-1终端");
        list.add(resourceTreeDataObjectVo);
        clientTreeForDBVo.setItem(list);
        List<ClientTreeForDBVo> clientTreeForDBVos = Lists.newArrayList();
        clientTreeForDBVos.add(clientTreeForDBVo);
        clientForDBVo.setLegalTerminal(clientTreeForDBVos);
        //未知
        ClientTreeForDBVo clientTreeForDBVo2 = new ClientTreeForDBVo();
        clientTreeForDBVo2.setId("A2");
        clientTreeForDBVo2.setName("数据库直连终端（未知）");
        clientTreeForDBVo2.setNumber(3);
        List<ResourceTreeDataObjectVo> list2 = Lists.newArrayList();
        ResourceTreeDataObjectVo resourceTreeDataObjectVo2 = new ResourceTreeDataObjectVo();
        resourceTreeDataObjectVo2.setId("B2");
        resourceTreeDataObjectVo2.setName("ZXY-3042");
        list2.add(resourceTreeDataObjectVo2);
        clientTreeForDBVo2.setItem(list2);
        clientForDBVo.setIllegalTerminal(clientTreeForDBVo2);
        return clientForDBVo;
    }

    /**
     * 更新客户端信息
     * @param dataMap
     * @return
     */
    @Override
    public Integer updateClientInfo(Data dataMap) {
        return null;
    }

    /**
     * 获得客户端信息  通过ID
     * @param dataMap
     * @return
     */
    @Override
    public ClientDB getClientInfoById(Data dataMap) {
        return null;
    }

    //===============================yyangs end====================//
}
